package com.eascs.admin.o2o.financial.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.detail.form.ShowDetailForm;
import com.eascs.admin.o2o.financial.service.O2oFinancialService;
import com.eascs.admin.o2o.financial.vo.O2oFinancialVo;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.type.SystemModule;

@Controller
@RequestMapping("/financial")
public class O2oFinancialController extends BaseMasterViewController {
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "Financial");
	}

	@Autowired
	O2oFinancialService financialService;
	@Reference
	SysOrgService sysOrgService;
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	private BaseUtils baseUtils;

	/**
	 * @Description:
	 *               <p>
	 *               财务查询
	 *               </p>
	 * @author LMX
	 * @date 2018年1月3日 下午3:56:31
	 */
	@RequestMapping("/index")
	public String index(O2oFinancialVo vo) {
		vo.setPageSize(10);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		String url = "/financial/index";
		String loanBank = vo.getLoanBank() == null ? "" : vo.getLoanBank();
		String loanBillCode = vo.getLoanBillCode() == null ? "" : vo.getLoanBillCode();
		String loanUserName = vo.getLoanUserName() == null ? "" : vo.getLoanUserName();
		String provinceCode = vo.getProvinceCode() == null ? "" : vo.getProvinceCode();
		String operatorName = vo.getOperatorName() == null ? "" : vo.getOperatorName();
		String loanDateStartTime = vo.getLoanDateStartTime() == null ? "" : vo.getLoanDateStartTime();
		String loanDateEndTime = vo.getLoanDateEndTime() == null ? "" : vo.getLoanDateEndTime();
		String endBankDateStartTime = vo.getEndBankDateStartTime() == null ? "" : vo.getEndBankDateStartTime();
		String endBankDateEndTime = vo.getEndBankDateEndTime() == null ? "" : vo.getEndBankDateEndTime();
		String loanType = vo.getLoanType() == null ? "" : vo.getLoanType();
		String wfType = vo.getWfType() == null ? "" : vo.getWfType();
		String ltdName = vo.getLtdName() == null ? "" : vo.getLtdName();
		url = UrlUtil.appendUrl(url, "loanBank", loanBank);
		url = UrlUtil.appendUrl(url, "loanBillCode", loanBillCode);
		url = UrlUtil.appendUrl(url, "loanUserName", loanUserName);
		url = UrlUtil.appendUrl(url, "provinceCode", provinceCode);
		url = UrlUtil.appendUrl(url, "operatorName", operatorName);
		url = UrlUtil.appendUrl(url, "loanDateStartTime", loanDateStartTime);
		url = UrlUtil.appendUrl(url, "loanDateEndTime", loanDateEndTime);
		url = UrlUtil.appendUrl(url, "endBankDateStartTime", endBankDateStartTime);
		url = UrlUtil.appendUrl(url, "endBankDateEndTime", endBankDateEndTime);
		url = UrlUtil.appendUrl(url, "loanType", loanType);
		url = UrlUtil.appendUrl(url, "wfType", wfType);
		url = UrlUtil.appendUrl(url, "ltdName", ltdName);
		url = url + "&pageIndex=";
		PageResult<Object[]> pageResult = financialService.findByPage(vo);//分页查询_财务查询包含接力贷、分期贷_最新
		String titles[] = { "贷款单号", "借款人", "信贷经理", "放款银行", "经营平台", "省区", "贷款类型", "放款金额(万元)", "放款日期", "到期日期", "还款状态" };
		SelectDataOption[] selectDataOptions = { null, null, null, new SelectDataOption("BANKTYPEO2O"), null, null, new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, new SelectDataOption("HKZT") };//主流程状态SQZT
		Button[] titleButton = new Button[] { ButtonHelper.createOpenButton(null, "/financial/detail_financial?loanapplyNo=") };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, url);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null, null, null, new DecimalFormat(",##0.00"), null, null, null });
		o2oPagingInfo.setTitleButtons(titleButton);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "financial/index";
	}
	
	
	/**
	 * @Description:
	 *               <p>
	 *                财务查询详情页
	 *               </p>
	 * @author LMX
	 * @date 2018年1月3日 下午3:57:13
	 */
	@RequestMapping(value = "/detail_financial", method = RequestMethod.GET)
	public String showDetail(ShowDetailForm showDetailForm) {
		O2oLoanapplyEntity loanapply = null;
		
		if (!StringUtil.isEmpty(showDetailForm.getBillCode())) {
			loanapply = o2oloanapplyService.fingByBillCodeDetail(showDetailForm.getBillCode());
			if (loanapply == null) {
				//没有权限返回错误页面
				return "notauthView";
			}
			showDetailForm.setLoanapplyNo(loanapply.getLoanapplyNo());
		}
		loanapply = o2oloanapplyService.findByIdDetail(showDetailForm.getLoanapplyNo());
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(showDetailForm.getLoanapplyNo()));
		
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		
		// 审批详情
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapply.getLoanapplyNo(), null, SystemModule.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		
		O2oLoanUserEntity loanSorpouseUser = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanSorpouseUser", loanSorpouseUser);
		
		return "financial/financialDetail";
	}

}
